home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / list_dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  1.6 KB  |  83 lines

  1. /*
  2.  * Do a shell escape with the "ls" command.  Additional command line options
  3.  * are allowed at run time.
  4.  */
  5.  
  6. #define LS_CMD "ls -aC"
  7.  
  8. #include <stdio.h>
  9. #include <curses.h>
  10. #include "config.h"
  11. #include "misc.h"
  12.  
  13. void
  14. list_dir()
  15. {
  16.     extern int fd;
  17.     WINDOW *ls_win, *newwin();
  18.     FILE *pfp, *n_popen();
  19.     int lines, oops;
  20.     char *ans, *cwd, *getcwd(), buf[200], *get_str();
  21.  
  22.     ls_win = newwin(6, 70, 8, 5);
  23.  
  24.     cwd = getcwd(buf, 200);
  25.  
  26.     mvwprintw(ls_win, 2, 4, "Current directory: %s", cwd);
  27.     mvwaddstr(ls_win, 3, 4, "File spec (wildcards allowed): ");
  28.     box(ls_win, VERT, HORZ);
  29.  
  30.     mvwattrstr(ls_win, 0, 3, A_BOLD, " List Directory ");
  31.     wmove(ls_win, 3, 35);
  32.     wrefresh(ls_win);
  33.  
  34.     if ((ans = get_str(ls_win, 80, "", "\n")) == NULL) {
  35.         if (fd == -1) {
  36.             werase(ls_win);
  37.             wrefresh(ls_win);
  38.         }
  39.         delwin(ls_win);
  40.         return;
  41.     }
  42.                     /* popen() an ls */
  43.     sprintf(buf, "%s %s", LS_CMD, ans);
  44.     pfp = n_popen(buf, "r");
  45.                     /* make a bigger window */
  46.     werase(ls_win);
  47.     wrefresh(ls_win);
  48.     delwin(ls_win);
  49.     ls_win = newwin(LINES-1, COLS, 0, 0);
  50.     touchwin(ls_win);
  51.                     /* a crude kind of paging */
  52.     oops = 0;
  53.     lines = 0;
  54.     while (fgets(buf, BUFSIZ, pfp) != NULL) {
  55.         waddstr(ls_win, buf);
  56.         lines++;
  57.         if (lines == LINES-2) {
  58.             lines = 0;
  59.             mvwaddstr(ls_win, LINES-2, 28, "Press any key for more");
  60.             wrefresh(ls_win);
  61.             if (wgetch(ls_win) == ESC) {
  62.                 oops++;
  63.                 break;
  64.             }
  65.             werase(ls_win);
  66.             wrefresh(ls_win);
  67.         }
  68.     }
  69.     n_pclose(pfp);
  70.  
  71.     if (!oops) {
  72.         mvwaddstr(ls_win, LINES-2, 25, "Press any key to continue");
  73.         wrefresh(ls_win);
  74.         wgetch(ls_win);
  75.     }
  76.     if (fd == -1) {
  77.         werase(ls_win);
  78.         wrefresh(ls_win);
  79.     }
  80.     delwin(ls_win);
  81.     return;
  82. }
  83.